<template>
  <div>
    <el-select v-model="is_push" placeholder="请选择发布类型">
      <el-option v-for="item in [{value:0,label:'导出'},{value:1,label:'发布'}]"
        :key="item.value" :label="item.label" :value="item.value">
      </el-option>
    </el-select>
    <el-button @click="ImportJson(is_push)" class="import" type="success">导出和发布</el-button>


    <el-tag type="warning">排期配置方案</el-tag>

    <el-table :data="tableData" style="width: 100%" highlight-current-row>
      <el-table-column label="" width="50">
        <template slot-scope="scope">
          <el-radio class="radio" v-model="radio" :label="scope.$index"
            @change.native="getCurrentRow(scope.$index,scope.row)">&nbsp;
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="Project" prop="project">
      </el-table-column>
      <el-table-column label="remark" prop="remark">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark" placeholder="remark"></el-input>
        </template>
      </el-table-column>
      <el-table-column align="right">
        <template slot-scope="scope">
          <el-button size="mini" type="warning" @click="handleRemark(scope.$index, scope.row)"
            :disabled="scope.$index==radio?false:true">Remark</el-button>
          <el-button size="mini" type="primary" @click="handleClone(scope.$index, scope.row)"
            :disabled="scope.$index==radio?false:true">克隆</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
            :disabled="scope.$index==radio?false:true">删除</el-button>
          <el-button size="mini" type="info" @click="handleView(scope.$index, scope.row)"
            :disabled="scope.$index==radio?false:true">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  import {
    fetchList
  } from '@/api/index'
  export default {
    data() {
      return {
        listQuery: {
          'action': 'extreme_user_event_schedule',
          'project': 0,
          'type': 'index',
          'data': []
        },
        tableData: [],
        search: '',
        is_push: 0,
        radio: -1,
        listLoading: true,
        fileList: []
      }
    },
    methods: {
      getList() {
        this.listLoading = true
        this.$set(this.listQuery, 'type', 'index')
        this.$set(this.listQuery, 'project', -1)
        fetchList(this.listQuery).then(response => {
          this.tableData = response.data
          this.listLoading = false
        })
      },
      handleView(index, row) {
        if (this.project != '') {
          let routeData = this.$router.resolve({
            path: "/" + this.listQuery.action + "/basic/",
            query: {
              project: this.listQuery.project
            },
          });
          window.open(routeData.href, '_blank');
        }
      },
      handleClone(index, row) {
        this.$confirm('克隆PID=' + this.listQuery.project + ', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          this.$set(this.listQuery, 'type', 'clone')
          fetchList(this.listQuery).then(response => {
            if (response.code === 0) {
              this.getList()
              this.$notify({
                title: 'Success',
                message: response.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '错误',
                message: response.msg
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消克隆'
          });
        });
      },
      handleDelete(index, row) {
        this.$confirm('删除PID=' + this.project + ', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$set(this.listQuery, 'type', 'delete')
          fetchList(this.listQuery).then(response => {
            if (response.code === 0) {
              this.getList()
              this.$notify({
                title: 'Success',
                message: response.msg,
                type: 'success',
                duration: 2000
              })

            } else {
              this.$notify.error({
                title: '错误',
                message: response.msg
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      getCurrentRow(index, row) {
        this.$set(this.listQuery, 'project', row.project)
        this.radio = index
      },
      ImportJson(is_push) {
        if (is_push == 0) {
          window.location.href = this.$settings.HOST + "/" + this.listQuery.action + "_down/" + is_push
        }else if (is_push == 1) {
          this.axios.get(this.$settings.HOST + "/" + this.listQuery.action + "_down/" + is_push)
            .then(response => {
              // console.log(response)
              this.$message({
                message: '上传成功',
                type: 'success',
                showClose: true,
                offset: 300
              });
              return false
            }).catch(function(error) {console.log(error);});
        } else if (is_push == 3) {
          this.axios.get(this.$settings.HOST + "/" + this.listQuery.action + "_down/" + is_push)
            .then(response => {
              if (response.data.ret == 0) {
                this.$message({
                  message: '上传成功',
                  type: 'success',
                  showClose: true,
                  offset: 300
                });
              } else {
                this.$message({
                  type: 'error',
                  message: response.data.msg
                });
              }
              return false
            }).catch(function(error) {console.log(error);});
        }
      },
      handleRemark(index, row) {
        this.$set(this.listQuery, 'type', 'remark')
        this.$set(this.listQuery, 'remark', row.remark)
        this.$set(this.listQuery, 'project', row.project)
        fetchList(this.listQuery).then(response => {
          if (response.code === 0) {
            // this.getList()
            this.$notify({
              title: 'Success',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
          } else {
            this.$notify.error({
              title: '错误',
              message: response.msg
            });
          }
        })
      }
    },
    created() {
      this.getList()
    }
  }
</script>

<style scoped>
</style>
